package com.cet.qz.unit01.chapter1_3;

import edu.princeton.cs.algs4.In;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-04-09 22:25
 **/
public class Date {
    private final int year;

    private final int month;

    private final int day;

    public Date(String date) {
        String[] fields = date.split("/");
        this.day = Integer.parseInt(fields[0]);
        this.month = Integer.parseInt(fields[1]);
        this.year = Integer.parseInt(fields[2]);
    }

    public static Date[] readDates(String name) {
        In in = new In(name);
        Queue<Date> queue = new Queue<>();
        while (!in.isEmpty()) {
            queue.enqueue(new Date(in.readString()));
        }
        int N = queue.size();
        Date[] a = new Date[N];
        for (int i = 0; i < N; i++) {
            a[i] = queue.dequeue();
        }
        return a;
    }

    @Override
    public String toString() {
        return "Date{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    public static void main(String[] args) {
        Date[] dates = readDates("src/com/cet/qz/data/dates1-3-16.txt");
        for (Date date : dates) {
            System.out.println(date);
        }
    }
}
